package com.garlic.face.jlf.transaction;


import com.garlic.face.jlf.transaction.bean.PayFlow;
import com.garlic.face.jlf.transaction.bean.SaleFlow;
import com.garlic.face.jlf.transaction.bean.TradeHead;
import com.garlic.face.jlf.transaction.bean.TradePay;
import com.garlic.face.jlf.transaction.dataInterfaces.CancelBarcodeInfo;
import com.garlic.face.jlf.transaction.dataInterfaces.CancelTradeInfo;
import com.garlic.face.jlf.transaction.dataInterfaces.GoodsInfo;
import com.garlic.face.jlf.transaction.dataInterfaces.LoginInfo;
import com.garlic.face.jlf.transaction.dataInterfaces.StateInfo;
import com.garlic.face.jlf.transaction.dataInterfaces.SubTotalInfo;
import com.garlic.face.jlf.transaction.dataInterfaces.TradeInfo;
import com.garlic.face.jlf.transaction.dataInterfaces.VipInfo;
import com.garlic.face.jlf.transaction.shop.PayInfo;
import com.garlic.face.jlf.utils.Tools;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;

public class Means {
    private static final String TAG = "Means";
    private static Means means = null;

    public static Means getInstance() {
        if (null == means) {
            synchronized (Means.class) {
                if (null == means) {
                    means = new Means();
                }
            }
        }
        return means;
    }
    //开关选项用于控制调试时设备启用或不用
    public static boolean blLampAlarmOK = false;    //警示灯开关
    public static boolean blScaleOK = false;        //电子秤开关
    public static boolean blScannerOK = false;      //扫描平台开关
    public static boolean blPrinterOK = false;      //打印机开关
    //数据库相关
    public static String connectStr = "jdbc:mysql://192.168.1.40:3306/cclcloud?useUnicode=true&characterEncoding=utf-8&Pooling=true";
    public static String dbName = "zzpos";
    public static String dbPwd = "123456";
    private boolean loginServer;
    private String ShopName = "";//店铺名
    private String Shopno = "";//店铺号
    private String Posno = "";//款台号
    private String PosIp = "";//通讯IP
    private int PosPort = 2000;//通讯端口
    private String deviceNum = "";//机具号
    private String LoginCashier = "";//已登录的收银员
    private String LoginCashierPwd = "";//已登录收银员密码
    private String VoidsIntervention = "";//0:取消单品/交易不需要授权 1:取消单品/交易需要授权 注意：只有在没有partial payment的情况下才可以取消交易。
    private int position = 0;//当前选中的行号（取消单品时用）
    private String[] bags = new String[]{"A6940712000117", "0", "A6940712000124"};//小中大购物袋
    public static final int BAG_SIZE_SMALL = 0;
    public static final int BAG_SIZE_MIDDLE = 1;
    public static final int BAG_SIZE_BIG = 2;

    public static boolean blApproveWeight = false;//是否服务员确认重量; true-客服授权
    public static boolean blCancelTradeSucess = false;//小计状态下无法授权干预重量，增加此字段标记取消交易已成功
    public static boolean blAssistMode = Tools.getNeedCloseWeight();//商店模式；true-商店模式，屏蔽商品重量判断。
    //重量相关
    public static double TolerancePrecision = 0.01;//自助学习时，大于此重量的为有效重量，避免称不稳定或震动、风吹等因素
    public static double ToleranceDefaultPer = 0.2;//普通商品误差默认百分比20%
    public static double ToleranceDefault = 0.1;// 普通商品误差默认值100g
    public static double ToleranceNoScan = 0.03;//未扫描商品时误差许可默认值30g;为忽略因地面震动，风吹等原因导致称重量的变化
    public static double ToleranceWeightPLU = 0.2;//秤重商品时误差许可默认值200g
    public static double ToleranceScanSmall = 0.01;//扫描商品时,小重量商品判断延时最大值10g; 用于平衡秤稳定后还有10g误差。
    public static double WeightStep1 = 0.15;//商品重量段1，0<weight<150g, 误差为30g；重量>500g固定误差100g
    public static double WeightStep2 = 0.5;//商品重量段2，150g<=weight<500g, 误差为20%；重量>500g固定误差100g
    public static double ToleranceAbsStep1 = 0.03;//重量误差1，0<weight<150g, 误差为30g；
    public static double ToleranceRateStep2 = 0.2;//重量误差2，150<=weight<500g, 误差为20%；

    public static double InitWeight = 0;//初始重量即皮重
    public static double TotalWeight = 0;//当前电子称上的总重量（预期总重量）
    public static double BagWeight = 0;//购物袋重量
    public static double weightqty = 0;//当前电子秤实时重量

    public static String secretKey = "12281228";//权限卡秘钥
    //报警灯
    public static int Lamp_Index = 0;//当前报警灯颜色： 0-红灯；1-黄灯；2-绿灯
    public static int Lamp_State = 0;//当前报警灯状态： 0-关闭；1-打开；2-闪烁


    private int TendCount = 0;//付款序号
    private int ItemCount = 0;//商品序号

    private TradeHead Rec_Tran = new TradeHead();//总交易结构
    private TradePay Temp_Pay = new TradePay();//临时付款结构

    private ArrayList<PayFlow> PayHead = new ArrayList<PayFlow>();//付款明细列表
    private ArrayList<SaleFlow> ItemHead = new ArrayList<SaleFlow>();//商品明细列表

    private ArrayList<String> PrintBillHead = new ArrayList<String>();//ERP小票内容

    //自助与POS交互数据
    private LoginInfo loginInfo = new LoginInfo();
    private StateInfo stateInfo = new StateInfo();
    private GoodsInfo goodsInfo = new GoodsInfo();
    private TradeInfo tradeInfo = new TradeInfo();
    private SubTotalInfo subTotalInfo = new SubTotalInfo();
    private VipInfo vipInfo = new VipInfo();
    private CancelBarcodeInfo cancelBarcodeInfo = new CancelBarcodeInfo();
    private CancelTradeInfo cancelTradeInfo = new CancelTradeInfo();
    private PayInfo payInfo = new PayInfo();

    /// <summary>
    /// 生成32位全球唯一id
    /// </summary>
    /// <returns></returns>
    public static String GetGuid()
    {
        String result = UUID.randomUUID().toString();
        if (result.length() > 32)
        {
            result = result.substring(0, 32);
        }
        return result;
    }

    public void clear(){
        TendCount = 0;
        ItemCount = 0;
        Rec_Tran = new TradeHead();
        Temp_Pay = new TradePay();
        PayHead = new ArrayList<PayFlow>();
        if (ItemHead != null)
            ItemHead.clear();
        PrintBillHead = new ArrayList<String>();
    }

    public void setBags(String[] bags) {
        this.bags = bags;
    }

    public void setPosIp(String posIp) {
        PosIp = posIp;
    }

    public void setPosPort(int posPort) {
        PosPort = posPort;
    }

    public void setPosno(String posno) {
        Posno = posno;
    }

    public void setShopno(String shopno) {
        Shopno = shopno;
    }

    public String getShopName() {
        return ShopName;
    }

    public void setShopName(String shopName) {
        ShopName = shopName;
    }

    public String getDeviceNum() {
        return deviceNum;
    }

    public void setDeviceNum(String deviceNum) {
        this.deviceNum = deviceNum;
    }

    public void setVoidsIntervention(String voidsIntervention) {
        VoidsIntervention = voidsIntervention;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public void setItemCount(int itemCount) {
        ItemCount = itemCount;
    }

    public void setCancelBarcodeInfo(CancelBarcodeInfo cancelBarcodeInfo) {
        this.cancelBarcodeInfo = cancelBarcodeInfo;
    }

    public void setCancelTradeInfo(CancelTradeInfo cancelTradeInfo) {
        this.cancelTradeInfo = cancelTradeInfo;
    }

    public void setGoodsInfo(GoodsInfo goodsInfo) {
        this.goodsInfo = goodsInfo;
    }

    public void setItemHead(ArrayList<SaleFlow> itemHead) {
        ItemHead = itemHead;
    }

    public void setLoginCashier(String loginCashier) {
        LoginCashier = loginCashier;
    }

    public void setLoginCashierPwd(String loginCashierPwd) {
        LoginCashierPwd = loginCashierPwd;
    }

    public void setLoginInfo(LoginInfo loginInfo) {
        this.loginInfo = loginInfo;
    }

    public static void setMeans(Means means) {
        Means.means = means;
    }

    public void setPayHead(ArrayList<PayFlow> payHead) {
        PayHead = payHead;
    }

    public void setPrintBillHead(ArrayList<String> printBillHead) {
        PrintBillHead = printBillHead;
    }

    public void setRec_Tran(TradeHead rec_Tran) {
        Rec_Tran = rec_Tran;
    }

    public void setPayInfo(PayInfo payInfo) {
        this.payInfo = payInfo;
    }

    public void setStateInfo(StateInfo stateInfo) {
        this.stateInfo = stateInfo;
    }

    public void setSubTotalInfo(SubTotalInfo subTotalInfo) {
        this.subTotalInfo = subTotalInfo;
    }

    public void setTemp_Pay(TradePay temp_Pay) {
        Temp_Pay = temp_Pay;
    }

    public void setTendCount(int tendCount) {
        TendCount = tendCount;
    }

    public void setTradeInfo(TradeInfo tradeInfo) {
        this.tradeInfo = tradeInfo;
    }

    public void setVipInfo(VipInfo vipInfo) {
        this.vipInfo = vipInfo;
    }

    public String[] getBags() {
        return bags;
    }

    public int getPosPort() {
        return PosPort;
    }

    public String getPosIp() {
        return PosIp;
    }

    public String getPosno() {
        return Posno;
    }

    public String getShopno() {
        return Shopno;
    }

    public String getVoidsIntervention() {
        return VoidsIntervention;
    }

    public int getPosition() {
        return position;
    }

    public int getItemCount() {
        return ItemCount;
    }

    public ArrayList<PayFlow> getPayHead() {
        return PayHead;
    }

    public ArrayList<SaleFlow> getItemHead() {
        return ItemHead;
    }

    public ArrayList<String> getPrintBillHead() {
        return PrintBillHead;
    }

    public CancelBarcodeInfo getCancelBarcodeInfo() {
        return cancelBarcodeInfo;
    }

    public CancelTradeInfo getCancelTradeInfo() {
        return cancelTradeInfo;
    }

    public GoodsInfo getGoodsInfo() {
        return goodsInfo;
    }

    public int getTendCount() {
        return TendCount;
    }

    public LoginInfo getLoginInfo() {
        return loginInfo;
    }

    public static Means getMeans() {
        return means;
    }

    public PayInfo getPayInfo() {
        return payInfo;
    }

    public StateInfo getStateInfo() {
        return stateInfo;
    }

    public String getLoginCashier() {
        return LoginCashier;
    }

    public String getLoginCashierPwd() {
        return LoginCashierPwd;
    }

    public static String getTAG() {
        return TAG;
    }

    public TradeHead getRec_Tran() {
        return Rec_Tran;
    }

    public TradePay getTemp_Pay() {
        return Temp_Pay;
    }

    public SubTotalInfo getSubTotalInfo() {
        return subTotalInfo;
    }

    public TradeInfo getTradeInfo() {
        return tradeInfo;
    }

    public VipInfo getVipInfo() {
        return vipInfo;
    }

    public boolean isLoginServer() {
        return loginServer;
    }

    public void setLoginServer(boolean loginServer) {
        this.loginServer = loginServer;
    }

    @Override
    public String toString() {
        return "Means{" +
                "Shopno='" + Shopno + '\'' +
                ", Posno='" + Posno + '\'' +
                ", PosIp='" + PosIp + '\'' +
                ", PosPort=" + PosPort +
                ", LoginCashier='" + LoginCashier + '\'' +
                ", LoginCashierPwd='" + LoginCashierPwd + '\'' +
                ", VoidsIntervention='" + VoidsIntervention + '\'' +
                ", position=" + position +
                ", bags=" + Arrays.toString(bags) +
                ", TendCount=" + TendCount +
                ", ItemCount=" + ItemCount +
                ", Rec_Tran=" + Rec_Tran +
                ", Temp_Pay=" + Temp_Pay +
                ", PayHead=" + PayHead +
                ", ItemHead=" + ItemHead +
                ", PrintBillHead=" + PrintBillHead +
                ", loginInfo=" + loginInfo +
                ", stateInfo=" + stateInfo +
                ", goodsInfo=" + goodsInfo +
                ", tradeInfo=" + tradeInfo +
                ", subTotalInfo=" + subTotalInfo +
                ", vipInfo=" + vipInfo +
                ", cancelBarcodeInfo=" + cancelBarcodeInfo +
                ", cancelTradeInfo=" + cancelTradeInfo +
                ", payInfo=" + payInfo +
                '}';
    }
}
